package com.study.base.performance;

/**
 * Description:
 * User: Administrator
 * DateTime: 2017/7/15 0015 12:21
 */
//How Expensive is Thread.getStackTrace()?  https://stackoverflow.com/questions/2347828/how-expensive-is-thread-getstacktrace

public class CurrentThreadTest {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < Count.count; i++) {
            getCurrentThread();
        }
        long endTime = System.currentTimeMillis();

        System.out.println("getCurrentThread  " + (endTime - startTime));


        startTime = System.currentTimeMillis();
        for (int i = 0; i < Count.count; i++) {
            getCurrentStackTrace();
        }
        endTime = System.currentTimeMillis();

        System.out.println("getCurrentStackTrace  " + (endTime - startTime));
        startTime = System.currentTimeMillis();
        for (int i = 0; i < Count.count; i++) {
            getCurrentMethodName();
        }
        endTime = System.currentTimeMillis();

        System.out.println("getCurrentMethodName  " + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for (int i = 0; i < Count.count; i++) {
            Thread.currentThread().getStackTrace()[0].getMethodName();
        }
        endTime = System.currentTimeMillis();

        System.out.println("getStackTrace  " + (endTime - startTime));

        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    static Thread getCurrentThread() {
        return Thread.currentThread();
    }

    static StackTraceElement getCurrentStackTrace() {
        return Thread.currentThread().getStackTrace()[2];
    }

    static String getCurrentMethodName() {
        return Thread.currentThread().getStackTrace()[2].getMethodName();
    }
}
